import handleNum from "./index";

let reducer = (
    state = {
        ...handleNum.state, //解构赋值
    },
    action: { type: string; value: number }
) => {
    let newState = JSON.parse(JSON.stringify(state)); //深拷贝
    // switch (action.type) {
    //     case handleNum.add1: {
    //         handleNum.actions[handleNum.add1](newState, action);
    //         break;
    //     }
    //     case handleNum.add2: {
    //         handleNum.actions[handleNum.add2](newState, action);
    //         break;
    //     }
    //     default:
    //         break;
    // }
    // 优化上述代码 添加action不需要手动更改reducer代码
    for (let key in handleNum.actionNames) {
        if (action.type === key) {
            handleNum.actions[handleNum.actionNames[key]](newState, action);
            break;
        }
    }
    // 尝试优化直接取actions 失败
    // for (let key in handleNum.actions) {
    //     if (action.type === key) {
    //         handleNum.actions[action.type];
    //         break;
    //     }
    // }
    return newState;
};
export default reducer;
